/**********************************************************************************
 ***
 ***    Screencasting Teaching System C++ Library
 ***    Copyright (C) 2011  Jiri Novak <jiri.novak@petriny.net>
 ***                        Wadi Jalil Maluf <wadijm@gmail.com>
 ***
 ***    This file is part of the Screencasting Teaching System C++ Library.
 ***
 ***    This library is free software; you can redistribute it and/or modify
 ***    it under the terms of the GNU General Public License as published by
 ***   the Free Software Foundation; either version 3 of the License, or
 ***    (at your option) any later version.
 ***
 ***    This library is distributed in the hope that it will be useful,
 ***    but WITHOUT ANY WARRANTY; without even the implied warranty of
 ***    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ***   GNU General Public License for more details.
 ***
 ***    You should have received a copy of the GNU General Public License
 ***    along with this library; if not, write to the Free Software
 ***    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 ***
 *********************************************************************************/

#include "login_dialog.h"
#include "../serverservice.h"
#include <cassert>
#include <iostream>
#include <cstdlib>

/**
 *
 */
LoginDialog::LoginDialog(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& builder) :
	Gtk::Dialog(cobject), builder_(builder) {
	initializeWidgets();
	makeConnections();
}

/**
 *
 */
void LoginDialog::initializeWidgets() {
	buttonCancel_ = 0;
	buttonOk_ = 0;
	entryUsername_ = 0;
	entryPassword_ = 0;
	entryServer_ = 0;
	entryPort_ = 0;

	builder_->get_widget("login_button_cancel", buttonCancel_);
	builder_->get_widget("login_button_ok", buttonOk_);
	builder_->get_widget("login_entry_username", entryUsername_);
	builder_->get_widget("login_entry_password", entryPassword_);
	builder_->get_widget("login_entry_server", entryServer_);
	builder_->get_widget("login_entry_port", entryPort_);

	assert(buttonCancel_ != 0 && "buttonCancel_ pointer is null!");
	assert(buttonOk_ != 0 && "buttonOk_ pointer is null!");
	assert(entryUsername_ != 0 && "entryUsername_ pointer is null!");
	assert(entryPassword_ != 0 && "entryPassword_ pointer is null!");
	assert(entryServer_ != 0 && "entryServer_ pointer is null!");
	assert(entryPort_ != 0 && "entryPort_ pointer is null!");
}

/**
 *
 */
void LoginDialog::makeConnections() {
	buttonCancel_->signal_clicked().connect(sigc::mem_fun(*this, &LoginDialog::onCancelRequested));
	buttonOk_->signal_clicked().connect(sigc::mem_fun(*this, &LoginDialog::onConfirmationRequested));
	this->signal_key_release_event().connect(sigc::mem_fun(*this, &LoginDialog::onKeyPressed));
}

/**
 *
 */
bool LoginDialog::onKeyPressed(GdkEventKey *event) {
	if (event->keyval == GDK_KEY_Return)
		onConfirmationRequested();
	return true;
}

/**
 *
 */
void LoginDialog::onCancelRequested() {
	hide();
}

/**
 *
 */
void LoginDialog::showErrorMessageDialog(const Glib::ustring &primary, const Glib::ustring &secondary) {
	Gtk::MessageDialog messageDialog(*this, primary, false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, false);
	messageDialog.set_secondary_text(secondary);
	messageDialog.run();
}

/**
 *
 */
void LoginDialog::onConfirmationRequested() {
	Glib::ustring username = entryUsername_->get_text();
	Glib::ustring password = entryPassword_->get_text();
	Glib::ustring server = entryServer_->get_text();
	Glib::ustring port = entryPort_->get_text();

	// check all the fields are filled
	Glib::ustring message = "";
	if (username.empty())
		message.append("Username not specified!\n");
	if (password.empty())
		message.append("Password not specified!\n");
	if (server.empty())
		message.append("Server not specified!\n");
	if (port.empty())
		message.append("Port not specified!\n");

	if (!message.empty()) {
		showErrorMessageDialog("Not all the required fields specified!",
				message);
		return;
	}

	// try to initialize connection
	if (!ServerService::getInstance().initComunication(server,
			atoi(port.c_str()))) {
		showErrorMessageDialog("Error while trying to connect!",
				"Unable to initialize connection, revise the server url and port!");
		return;
	}

	// try to login
	ServerService::getInstance().login(username, password);
}

/**
 *
 */
void LoginDialog::onLoginSuccessfull() {
	hide();
}

/**
 *
 */
void LoginDialog::onLoginUnsuccessfull() {
	Glib::signal_idle().connect_once(sigc::bind(sigc::mem_fun(*this, &LoginDialog::showErrorMessageDialog), "Authentication process failed!", "Login unsuccessfull, revise your credentials!"));
}
